import 'dart:io';

import 'package:dio/adapter.dart';
import 'package:dio/dio.dart';
import 'package:loxen/api/intercept.dart';
import 'package:loxen/utils/config.dart';

class BaseNetWork {
  // 工厂模式
  factory BaseNetWork() => _getInstance();

  static BaseNetWork get instance => _getInstance();
  static BaseNetWork _instance;
  Dio dio;
  BaseOptions options;

  BaseNetWork._internal() {
    dio = Dio()
      ..options = BaseOptions(
          baseUrl: Constants.BASE_URL,
          connectTimeout: 10000,
          receiveTimeout: 1000 * 60 * 60 * 24,
          responseType: ResponseType.json,
          headers: {"Content-Type": "application/json"})
      //网络状态拦截
      ..interceptors.add(AuthInterceptor())
      // ..interceptors.add(HttpLog())
      ..interceptors.add(ErrorInterceptor());
//      ..interceptors.add(DioLogInterceptor());
    //拦截器
//       (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
//         (HttpClient client) {
//       client.findProxy = (uri) {
//         //proxy all request to localhost:8888
//         return 'PROXY 192.168.31.34:8888';
//       };
//       client.badCertificateCallback =
//           (X509Certificate cert, String host, int port) => true;
//     };

    //..interceptors.add(LogInterceptor(responseBody: true, requestBody: true));
  }

  static BaseNetWork _getInstance() {
    if (_instance == null) {
      _instance = new BaseNetWork._internal();
    }
    return _instance;
  }
}
